package cn.jdy.lhkgaj.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.lhkgaj.entity.Duty;
import cn.jdy.lhkgaj.service.DutyService;
import cn.jdy.platform.entity.ConfValue;

/**
 * @author yym
 * @title 通知通报控制类
 * @date 2016/02/16
 */
@Controller
@RequestMapping("/duty")
public class DutyCtrl extends BaseCtrl {

	@Autowired
	private DutyService newsService;
	//默认列表路径
	private static String viewName = "redirect:/Duty/list?s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Duty entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/operate/dutyList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		return mv;
	}

	/**
	 * 异步查询
	 * @param news
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Duty columns, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", newsService.query2(columns, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", newsService.count2(columns, fromDate, toDate));
			jsonMap.put("iTotalRecords", newsService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/dutyDetail.jsp");
		Duty datas = this.newsService.find(id);
		Duty datanewsNews = new Duty();
		List<ConfValue> confvalue = confService.find("typeid",65L,"pid",10171L);
		mv.addObject("data", id==null||id==0 ? datanewsNews : datas);
		mv.addObject("dutyList", confvalue);
		return mv;
	}

	/**
	 * 保存（更新）新闻
	 * @param news
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(Duty columns, Long pid) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(columns.getId()==null) {
				// 新增通知通报
				this.newsService.save(columns);
				mv.setViewName(this.viewName.concat("1"));
				jsonMap.put("statusCode", 100);
			} else {
				//更新修改通知通报
				// 新增通知通报
				User user = getUser();
				this.newsService.update(columns);
				mv.setViewName(this.viewName.concat("2"));
				jsonMap.put("statusCode", 200);
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, columns.getId());
			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return jsonMap;
	}
	
	/**
	 * 删除新闻
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.newsService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理文章缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}

	
	
	
	
}
